package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.salary.entity.FileInfo;
import com.boss.salary.service.IFileInfoService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fileInfoController")
public class FileInfoController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(FileInfoController.class);
    public static final String SUCCESS_CODE = "100000";
    @Autowired
    private IFileInfoService iFileInfoService;

    /**
     * 新增
     * @param fileInfo
     * @return
     */
    @PostMapping("/insertFileInfo")
    public ApiResultDTO insertFileInfo(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody FileInfo fileInfo){
        try {
            UserDTO userDTO = getUser(tokenid);
            fileInfo.setMofDivCode(userDTO.getProvince());
            String result = iFileInfoService.insertFileInfo(fileInfo);
            return ApiResultDTO.success(result, "保存成功");
        } catch (Exception e) {
            logger.error("新增 附件发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 附件发生异常");
        }
    }


    /**
     * 批量新增附件
     * @param fileInfoList
     * @return
     */
    @PostMapping("/insertFileInfoList")
    public ApiResultDTO insertFileInfoList(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody List<FileInfo> fileInfoList){

        try {
            UserDTO userDTO = getUser(tokenid);
            String result=iFileInfoService.insertFileInfoList(fileInfoList,userDTO);

            return ApiResultDTO.success(result, "保存成功");
        } catch (Exception e) {
            logger.error("新增 附件发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 附件发生异常");
        }
    }
    /**
     * 根据id删除
     * @param fileInfo
     * @return
     */
    @PostMapping("/deleteFileInfo")
    public ApiResultDTO deleteFileInfo(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody FileInfo fileInfo){
        try {
            String result=iFileInfoService.deleteFileInfo(fileInfo);
            return ApiResultDTO.success(result, "删除成功");
        } catch (Exception e) {
            logger.error("删除 附件发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 附件发生异常");
        }
    }


    /**
     * 根据id批量删除
     * @param id
     * @return
     */
    @PostMapping("/deleteEnclosureByIds")
    public ApiResultDTO deleteEnclosureByIds( @RequestBody List<String> id){
        try {
            String result=iFileInfoService.deleteEnclosureByIds(id);
            return ApiResultDTO.success(result, "删除成功");
        } catch (Exception e) {
            logger.error("删除 附件发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 附件发生异常");
        }
    }


    /**
     * 查询
     * @return
     */
    @PostMapping("/getFileInfo")
    public ApiResultDTO getFileInfo(@RequestHeader("X-Token") String tokenid,
                                    @RequestBody FileInfo fileInfop
                                     ){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<String> list=fileInfop.getList();
            String agencyId=fileInfop.getAgencyId();
            String operateType=fileInfop.getOperateType();
            if(!StringUtils.isEmpty(agencyId)){
                List<FileInfo> fileInfo = iFileInfoService.getFileInfo(list, operateType, agencyId,userDTO.getProvince(),userDTO);
                return ApiResultDTO.success(fileInfo);
            }else {
                return ApiResultDTO.error(ERROR_RSCODE, "查询 附件发生异常");
            }
        } catch (Exception e) {
            logger.error("查询 附件发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询 附件发生异常");
        }
    }
    /**
     * 查询
     * @return
     */
    @PostMapping("/queryExcel")
    public ApiResultDTO queryExcel(@RequestHeader("X-Token") String tokenid,
                                   @RequestBody FileInfo fileInfop) {
        try {
                 UserDTO userDTO = getUser(tokenid);
            List<String> list=fileInfop.getList();
            String agencyId=fileInfop.getAgencyId();
                List<FileInfo> fileInfo = iFileInfoService.queryExcel(list,userDTO.getProvince(),agencyId,userDTO);
                return ApiResultDTO.success(fileInfo);
        } catch (Exception e) {
            logger.error("查询 附件发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询 附件发生异常");

        }
    }
            /**
             * 查询附件数量
             * @param personId 传过来的id
             * @return 当前id的封装数量
             */
    @GetMapping("/queryPersonFileNum")
    public ApiResultDTO queryPersonFileNum(@RequestHeader("X-Token") String tokenid,
                                           @RequestParam(value = "personId") List<String> personId,
                                           @RequestParam(value = "agencyId") String agencyId,
                                           @RequestParam(value = "operateType") String operateType){
        try {
            UserDTO userDTO = getUser(tokenid);
            Integer count = iFileInfoService.queryPersonFileNum(personId, agencyId, operateType,userDTO.getProvince());
            return ApiResultDTO.success(count);
        } catch (Exception e) {
            logger.error("查询 附件数量发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询 附件发生异常");
        }
    }
}
